﻿using BCCommon;
using BCCommon.Enums;
using BCData.OA.WorkSchedule.WorkScheduleChildrenTask;
using BCDto.OA.WorkSchedule.WorkScheduleChildrenTask;
using BCEntity.OA.WorkSchedule.WorkScheduleChildrenTask;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCCommon.OAEnums.WorkSchedule;
using BCData.Sim.Company.Department;
using BCData.Sim.Company.Company;
using BCDto.Sim.Admin.Admin;

namespace BCService.OA.WorkSchedule.WorkScheduleChildrenTask
{
	public class WorkScheduleChildrenTaskService : IWorkScheduleChildrenTaskService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly ICompanyData companyData;
		private readonly IDepartmentData departmentData;
		private readonly IWorkScheduleChildrenTaskData workScheduleChildrenTaskData;
		private readonly string key = string.Format("{0}", EntityNameConst.WorkScheduleChildrenTaskEntity);

		public WorkScheduleChildrenTaskService(IDatabaseContext databaseContext,
			IRedisService redisService,
			ICompanyData companyData,
			IDepartmentData departmentData,
			IWorkScheduleChildrenTaskData workScheduleChildrenTaskData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.companyData = companyData;
			this.departmentData = departmentData;
			this.workScheduleChildrenTaskData = workScheduleChildrenTaskData;
		}

		#region 检查工作进度表子任务是否存在
		public bool Exists(long workScheduleChildrenTaskId)
		{
			return this.workScheduleChildrenTaskData.Exists(workScheduleChildrenTaskId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加工作进度表子任务
		public WorkScheduleChildrenTaskDto Add(WorkScheduleChildrenTaskRequestDto requestDto,AdminDto adminDto)
		{
			if (requestDto.ChildType == OAWorkScheduleType.Date)
			{
				if (!requestDto.StartDate.HasValue || !requestDto.EndDate.HasValue)
				{
					throw new ArgumentException("开始日期与结束日期都不应为空!");
				}
			}
			else
			{
				if (!requestDto.StartDateTime.HasValue || !requestDto.EndDateTime.HasValue)
				{
					throw new ArgumentException("开始日期时间与结束日期时间都不应为空!");
				}
			}
			if (!this.companyData.CheckCompanyExists(requestDto.CompanyId).GetAwaiter().GetResult())
			{
				throw new ArgumentException("此公司不存在!");
			}
			if (!this.departmentData.CheckDepartment(requestDto.DepartmentId).GetAwaiter().GetResult())
			{
				throw new ArgumentException("此部门不存在!");
			}
			var companyEntity = this.companyData.GetEntity(requestDto.CompanyId).GetAwaiter().GetResult();
			var departmentEntity = this.departmentData.GetById(requestDto.DepartmentId).GetAwaiter().GetResult();
			var entity = requestDto.As<WorkScheduleChildrenTaskEntity>();
			entity.CreateAdminName = adminDto.FullName;
			entity.CreateAdminId = adminDto.AdminId;
			entity.CompanyName = companyEntity.CompanyName;
			entity.DepartmentName = departmentEntity.DepartmentName;
			var result = this.workScheduleChildrenTaskData.Add(entity).GetAwaiter().GetResult().As<WorkScheduleChildrenTaskDto>();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 修改工作进度表子任务
		public WorkScheduleChildrenTaskDto Update(WorkScheduleChildrenTaskPutDto putDto,AdminDto adminDto)
		{
			if (putDto.ChildType == OAWorkScheduleType.Date)
			{
				if (!putDto.StartDate.HasValue || !putDto.EndDate.HasValue)
				{
					throw new ArgumentException("开始日期与结束日期都不应为空!");
				}
			}
			else
			{
				if (!putDto.StartDateTime.HasValue || !putDto.EndDateTime.HasValue)
				{
					throw new ArgumentException("开始日期时间与结束日期时间都不应为空!");
				}
			}
			if (!this.companyData.CheckCompanyExists(putDto.CompanyId).GetAwaiter().GetResult())
			{
				throw new ArgumentException("此公司不存在!");
			}
			if (!this.departmentData.CheckDepartment(putDto.DepartmentId).GetAwaiter().GetResult())
			{
				throw new ArgumentException("此部门不存在!");
			}
			if (!this.Exists(putDto.WorkScheduleChildrenTaskId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var companyEntity = this.companyData.GetEntity(putDto.CompanyId).GetAwaiter().GetResult();
			var departmentEntity = this.departmentData.GetById(putDto.DepartmentId).GetAwaiter().GetResult();
			var entity = putDto.As<WorkScheduleChildrenTaskEntity>();
			entity.EditAdminName = adminDto.FullName;
			entity.EditAdminId = adminDto.AdminId;
			entity.CompanyName = companyEntity.CompanyName;
			entity.DepartmentName = departmentEntity.DepartmentName;
			var result = this.workScheduleChildrenTaskData.Update(entity).GetAwaiter().GetResult().As<WorkScheduleChildrenTaskDto>();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 删除工作进度表子任务
		public bool Delete(long workScheduleChildrenTaskId)
		{
			if (!this.Exists(workScheduleChildrenTaskId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.workScheduleChildrenTaskData.Delete(workScheduleChildrenTaskId).GetAwaiter().GetResult();
			this.redisService.DeleteMultipleKey(key, false);
			return result;
		}
		#endregion

		#region 获取工作进度表子任务
		public WorkScheduleChildrenTaskDto Get(long workScheduleChildrenTaskId)
		{
			string cacheKey = this.redisService.GetKey(key, workScheduleChildrenTaskId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.workScheduleChildrenTaskData.GetEntity(workScheduleChildrenTaskId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<WorkScheduleChildrenTaskDto>();
		}
		#endregion

		#region 获取工作进度表子任务列表
		public IEnumerable<WorkScheduleChildrenTaskDto> GetList(long workScheduleId)
		{
			string cacheKey = this.redisService.GetKey(key, workScheduleId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.workScheduleChildrenTaskData.GetList(workScheduleId).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<WorkScheduleChildrenTaskDto>>();
		}
		#endregion

		#region 获取工作进度表子任务分页
		public PageableList<WorkScheduleChildrenTaskDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.workScheduleChildrenTaskData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<WorkScheduleChildrenTaskDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<WorkScheduleChildrenTaskDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion
	}
}